home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / image.ed / main10.c < prev    next >
C/C++ Source or Header  |  1985-10-26  |  14KB  |  440 lines

  1.  
  2. /************* main10.c ******************/
  3.  
  4. /* we will show 9 views totally as a 3 by 3 matrix ..... if there are
  5.  * more, we will scroll them into view, thunk! thunk! etc., showing
  6.  * 9 at a time, in a fixed space.
  7.  */     
  8.  
  9. #include "intuall.h"
  10. #include "imageedit.h"
  11.  
  12. #define SECONDS io_Actual
  13. #define MICROSECONDS io_Length
  14.  
  15. extern SHORT zeros[];
  16. extern struct Menu menu[]; 
  17. extern SHORT palette[];
  18. extern SHORT depth;
  19.  
  20. struct frame f[MAXVIEWS];
  21.  
  22. SHORT selected;         /* which frame number is cursor in? */
  23.  
  24. extern struct TextAttr TestFont;
  25. extern struct Requester myrequest; 
  26. extern BYTE dosname[];
  27.  
  28. long GfxBase = 0;
  29. SHORT framewidth, frameheight, editbottom, editright;
  30. SHORT bobdepth;
  31.  
  32. struct Window *OpenWindow();
  33. struct InputEvent *Intuition();
  34. struct Screen *OpenScreen();
  35. struct RastPort *CDRastPort();
  36.  
  37. extern struct MsgPort *CreatePort();
  38. extern struct IOStdReq *CreateStdIO();
  39.  
  40. struct MsgPort *timerport;              /* for timer communications */
  41. struct IOStdReq *timermsg;              /* for timer */
  42.  
  43. SHORT click, timeout;   /* use of these variables to prevent the
  44.                          * holding down of the mouse from accidently
  45.                          * selecting an alternate frame if the user
  46.                          * has moved the mouse into an alternate frame
  47.                          * without first lifting the left button */
  48.  
  49.  
  50. ULONG wakeupbit;        /* timer bit or event bit wake us up? */
  51.  
  52.  
  53. USHORT class;
  54. USHORT code;
  55. USHORT qualifier;
  56. USHORT mode;            /* mode of operation */
  57. APTR address;           /* address of the gadget which we hit */
  58.  
  59. struct Window *w;
  60. struct Screen *screen;
  61. struct RastPort *rp;
  62. struct ViewPort *vp;
  63. struct IntuiMessage *message;
  64.  
  65. extern HandleEvent();
  66.  
  67. long IntuitionBase=0;
  68.  
  69. struct NewScreen ns = {
  70.         0, 0,                   /* start position               */
  71.         640, 200, DEPTH,        /* width, height, depth         */
  72.         0, 1,                   /* detail pen, block pen        */
  73.         HIRES,                  /* viewing mode                 */
  74.         CUSTOMSCREEN,           /* screen type                  */
  75.         &TestFont,              /* font to use                  */
  76.         "Image Editor, v1.0",   /* default title for screen     */
  77.         NULL                    /* pointer to additional gadgets */
  78.         };
  79.  
  80. struct NewWindow nw = {
  81.         0, 0,                   /* start position               */
  82.         640, 200,               /* width, height                */
  83.         0, 1,                   /* detail pen, block pen        */
  84.         GADGETUP | GADGETDOWN | MOUSEBUTTONS | MENUPICK | RAWKEY,
  85.                                 /* IDCMP flags                  */
  86.         ACTIVATE | BACKDROP | BORDERLESS,       
  87.                                 /* window flags                 */
  88.         NULL,                   /* pointer to first user gadget */
  89.         NULL,                   /* pointer to user checkmark    */ 
  90.         NULL,                   /* window title         */ 
  91.         NULL,                   /* pointer to screen    (later) */
  92.         NULL,                   /* pointer to superbitmap       */
  93.         0,0,0,0,                /* ignored/not a sized window so
  94.                                  * dont have to specify min/max
  95.                                  * size to allow                */      
  96.         CUSTOMSCREEN            /* type of screen in which to open */   
  97.         };
  98.  
  99.  
  100. main()
  101. {
  102.         SHORT i,condition;
  103.         BYTE *s, *d, *defaultname;
  104.         depth = DEPTH;
  105.         for(i=0; i<20; i++)
  106.                 zeros[i]=0;
  107.         condition = TRUE;
  108.  
  109.  
  110.         /* here going to establish the values for editright and editbottom
  111.          * for the reconfiguring of the editor 
  112.          */
  113.         bobdepth = BOBDEPTH;
  114.         framewidth = FRAMEWIDTH;                /* establish the defaults */
  115.         frameheight = FRAMEHEIGHT;
  116.         editright = EDITLEFT + 4 * framewidth;
  117.         editbottom = EDITTOP + 4 * frameheight;
  118.  
  119.         /* the prior 2 equations will be used during reconfigure */     
  120.  
  121.  
  122.         GfxBase = OpenLibrary("graphics.library", 0);
  123.         if (GfxBase == NULL)
  124.         {
  125.                 printf("Unable to open graphics library\n");
  126.                 exit(1000);
  127.         }
  128.         IntuitionBase = OpenLibrary("intuition.library", 0);
  129.         if (IntuitionBase == NULL)
  130.         {
  131.                 printf("Unable to open intuition library\n");
  132.                 exit(1000);
  133.         }
  134.         timerport = CreatePort(0,0);
  135.         if (timerport == 0) { exit(997); }
  136.         timermsg = CreateStdIO(timerport);
  137.         if (timermsg == 0) { exit(996); }
  138.  
  139.         if (condition)
  140.         {
  141.                 screen = OpenScreen(&ns); 
  142.                 if (screen == NULL)
  143.                 {
  144.                         exit(1);
  145.                 }
  146.  
  147.                 nw.Screen = screen;     
  148.         
  149.                 w = OpenWindow(&nw);            /* open a window */
  150.                 rp = w->RPort;
  151.                 vp = &w->WScreen->ViewPort;
  152.  
  153. /*  Set the color registers  Black, Red, Green, Blue, Yellow, Magenta,
  154.         Cyan, White */
  155.  
  156.         if(palette[DEPTH-1] == 8)
  157.                 {
  158.                 SetRGB4(vp,0,00,00,00); 
  159.                 SetRGB4(vp,1,15,00,00);  
  160.                 SetRGB4(vp,2,00,15,00); 
  161.                 SetRGB4(vp,3,00,00,15);   
  162.                 SetRGB4(vp,4,15,15,00);
  163.                 SetRGB4(vp,5,15,00,15); 
  164.                 SetRGB4(vp,6,00,15,15);   
  165.                 SetRGB4(vp,7,15,15,15); 
  166.                 } 
  167.         else if (palette[DEPTH-1] == 16)
  168.                 {
  169.                 /* black, black, white, fireengine red, orange, yellow,
  170.                 lime green, green, aqua, dark blue, purple,
  171.                 violet, tan, brown, gray, skyblue */
  172.  
  173.                 SetRGB4(vp,0,00,00,00); 
  174.                 SetRGB4(vp,1,14,3,0);   
  175.                 SetRGB4(vp,2,15,15,15); 
  176.                 SetRGB4(vp,3,11,4,0);   
  177.                 SetRGB4(vp,4,15,11,0);  
  178.                 SetRGB4(vp,5,11,15,0);  
  179.                 SetRGB4(vp,6,5,13,0);   
  180.                 SetRGB4(vp,7,0,14,13);  
  181.                 SetRGB4(vp,8,7,13,15);  
  182.                 SetRGB4(vp,9,6,9,15);   
  183.                 SetRGB4(vp,10,12,0,14); 
  184.                 SetRGB4(vp,11,15,2,14); 
  185.                 SetRGB4(vp,12,15,13,11);        
  186.                 SetRGB4(vp,13,12,9,8);  
  187.                 SetRGB4(vp,14,11,11,11);        
  188.                 SetRGB4(vp,15,7,13,15); 
  189.                 } 
  190.         }
  191.         else
  192.         {
  193.                 exit(); /* couldn't close workbench */
  194.         }
  195.  
  196. /* ******************************************************************* */
  197.         InitMenu(); 
  198.  
  199.         InitDosRequest();               /* only mods to main3.c */
  200.         InitDosText();                  /* only mods to main3.c */
  201.         InitTextRequest();
  202.  
  203.         InitNewColorText();
  204.         InitColorRequest();
  205.  
  206.         defaultname = "images";
  207.         s = defaultname;
  208.         d = &dosname[0];
  209.         while((*d++ = *s++) != '\0')
  210.                                 ;       /* copy name into buffer */
  211.  
  212.         /* mods between here an InitDosRequest above */
  213. /* ******************************************************************* */
  214.  
  215.  
  216.         /* PrepareMenuStrip();  *** new format of strip init */ 
  217.  
  218. /*  Set up the Menu bar  */
  219.         if (!(SetMenuStrip(w, &menu[0])))
  220.                 {
  221.                 CloseWindow(w);
  222.                 CloseScreen(screen);
  223.                 exit(998);
  224.                 }
  225. /* Initialize the timer */
  226.         if (init_timer() != 0)  
  227.                 {
  228.                 CloseWindow(w);
  229.                 CloseScreen(screen);
  230.                 exit(1);
  231.                 }
  232.  
  233.         SetAPen(rp,2);  
  234.         SetDrMd(rp,JAM1);
  235.         initoutlines();
  236.  
  237.         workbitmaps();  /* init the work bit maps for expansion */
  238.  
  239.         ShowTitle(screen,TRUE);
  240.  
  241.         class=0;  code =0;  /* initialize */
  242.  
  243. /*  Wait for a CLOSEWINDOW */
  244.  
  245.         set_timer(0,70000);     
  246.  
  247. /* 70,000 microseconds = .07 sec */
  248.  
  249.         do {
  250.             click = FALSE;   timeout = FALSE;
  251.             message = (struct IntuiMessage *)GetMsg(w->UserPort);
  252.             if(message == NULL)         /* EMPTY THE PORT!!!!!, if intuition
  253.                                          * sent us two messages, both would
  254.                                          * have set the same signal bit....
  255.                                          * so we must respond to all messages
  256.                                          * rather than simply checking if
  257.                                          * a signal bit was set.
  258.                                          * (earlier version of program
  259.                                          * got stuck one message behind
  260.                                          * while responding to prior msg
  261.                                          * and simply going to sleep
  262.                                          * without emptying the port first 
  263.                                          */
  264.                 {
  265.                 wakeupbit = Wait( 1 << w->UserPort->mp_SigBit 
  266.                              |  1 << timerport->mp_SigBit  );
  267.             
  268.                 /* go to sleep waiting for click or timeout or both */
  269.                 /* if receive an intuion message, save its variables and reply
  270.                  * (make the message available for reuse by Intuition) */
  271.  
  272.                 if( wakeupbit & (1 << w->UserPort->mp_SigBit ))
  273.                         {
  274.                         click = TRUE;
  275.                         message = (struct IntuiMessage *)GetMsg(w->UserPort);
  276.                         if(message != NULL)     
  277.                                 {
  278.                                 class = message->Class;
  279.                                 code =  message->Code;
  280.                                 qualifier = message->Qualifier;
  281.                                 address = message->IAddress;
  282.                                 ReplyMsg(message);
  283.                                 }
  284.                         }       
  285.                 /* if wakened by a timeout, it may be time to write another
  286.                  * dot.  After each timeout, restore the time variables,ask to
  287.                  * start another timing interval */
  288.  
  289.                 if( wakeupbit & (1 << timerport->mp_SigBit ))
  290.                         {
  291.                         GetMsg(timerport);      
  292.                         timeout = TRUE;
  293.                         set_timer(0,70000);
  294.                         }
  295.                 }
  296.              else               /* this empties the port if more than
  297.                                  * one message arrives with a single
  298.                                  * signal bit 
  299.                                  */
  300.                 {
  301.                 class = message->Class;
  302.                 code =  message->Code;
  303.                 qualifier = message->Qualifier;
  304.                 address = message->IAddress;
  305.                 ReplyMsg(message);
  306.                 }
  307.  
  308.         } while (HandleEvent());        /* keep going as long as handleevent
  309.                                          * returns a true value */
  310.  
  311.         CloseWindow(w);
  312.         CloseScreen(screen); 
  313.         FreeMyMemory();         /* give back all allocated rasters to system */
  314.         DeleteStdIO(timermsg);
  315.         DeletePort(timerport);
  316.  
  317. }
  318.  
  319.  
  320. init_timer()
  321. {
  322.         return( OpenDevice(TIMERNAME,UNIT_VBLANK,timermsg,0));
  323. }
  324.  
  325.  
  326. set_timer(sec,micro)
  327. ULONG sec,micro;
  328. {
  329.         timermsg->io_Command = TR_ADDREQUEST;    /* add a new timer request */
  330.         timermsg->SECONDS = sec;        /* seconds */
  331.         timermsg->MICROSECONDS = micro; /* microseconds */
  332.         SendIO( timermsg );             /* post a request to the timer */
  333.         return(0);
  334. }
  335.  
  336.  
  337.  
  338. /* user has put the left button of the mouse down somewhere.  We must
  339.  *      a.  write a big pixel if he is in the edit window.
  340.  *      b.  select another window and copy its contents to the
  341.  *              big window for editing (only if button lifted first)
  342.  *      c.  do nothing (for now) if he is outside of all windows.
  343.  *
  344. */
  345. doclick(x,y)            
  346. SHORT x,y;
  347. {
  348.         SHORT i;
  349.         if(qr(x,EDITLEFT,editright-1) && qr(y,EDITTOP,editbottom-1))
  350.                          {
  351.                           bigpixel(x,y); 
  352.                           WritePixel(rp,f[selected].xmin+((x-EDITLEFT) >> 2),
  353.                                         f[selected].ymin+((y-EDITTOP)  >> 2)  );
  354.                           return(0); 
  355.                           }
  356.  
  357.         if( timeout == FALSE )
  358.         {
  359.             for(i=0; i<MAXVIEWS; i++)
  360.                 if(qr(x,f[i].xmin,f[i].xmax) && qr(y,f[i].ymin,f[i].ymax))
  361.                         { select(&f[i],i); break; }
  362.         }
  363.         return(0);
  364. }
  365.  
  366.  
  367. select(p,i)                     /* select a new object to work with */
  368. struct frame *p;
  369. SHORT i;
  370. {
  371. struct frame *q;                /* deselect the previous box */
  372. if( i == selected ) return(0);  /* dont erase if same one selected twice */
  373. erase_edit();
  374.  
  375. q = &f[selected]; 
  376. box(q->xmin-1, q->ymin-1, q->xmax+2, q->ymax+2, 7);
  377. selected = i;
  378. box(p->xmin-1, p->ymin-1, p->xmax+2, p->ymax+2, 2);
  379.  
  380. doexpand(i);
  381.  
  382. return(0); 
  383. }
  384.  
  385.  
  386. box(x,y,xh,yh,c)
  387. SHORT x,y,xh,yh,c;
  388. {
  389.         SHORT oldpen;
  390.         oldpen = rp->FgPen;
  391.         SetAPen(rp,c);
  392.         Move(rp,x,y);   Draw(rp,x,yh);  Draw(rp,xh,yh);  Draw(rp,xh,y); 
  393.         Draw(rp,x,y);
  394.         SetAPen(rp,oldpen);
  395.         return(0);
  396. }
  397.  
  398. erase_edit()            /* clear the edit window */
  399. {
  400. SHORT oldpen;
  401. oldpen = rp->FgPen;
  402. SetAPen(rp,0);
  403. SetDrMd(rp,JAM1);
  404. RectFill(rp,EDITLEFT,EDITTOP,editright+2,editbottom+2);
  405. box(EDITLEFT-1, EDITTOP-1, editright+1, editbottom+1,7);
  406. SetAPen(rp,oldpen);
  407. return(0);
  408. }
  409.  
  410. initoutlines()
  411. {
  412.         SHORT i,j,k;
  413.         SHORT x,y;
  414.         k=0;
  415.         y=23;
  416.         for(i=0; i<3; i++)
  417.                 {
  418.                 x = editright+9;
  419.                 for(j=0; j<3; j++)
  420.                         {
  421.                         box(x,y,x+framewidth+2,y+frameheight+2,7);
  422.                         f[k].xmin = x+1;
  423.                         f[k].ymin = y+1;
  424.                         f[k].xmax = x+framewidth;
  425.                         f[k].ymax = y+frameheight;
  426.                         x += framewidth+16;
  427.                         k++;
  428.                         }
  429.                 y += frameheight+8;
  430.                 }
  431.  
  432.         selected = 0;           /* force first one to get highlighted */
  433.         select(&f[0],0);        /* highlight the first one */
  434.         SetAPen(rp,0);
  435.         erase_edit();
  436.         box(f[0].xmin-1, f[0].ymin-1, f[0].xmax+2, f[0].ymax+2, 2);
  437.         return(0);
  438. }
  439.  
  440.